package com.test.deencredit.activity_deencredit;

import android.content.Intent;
import android.os.Bundle;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.test.deencredit.analytics_deencredit.timezone_deencredit.TimeZoneUtil_deencredit;
import com.test.deencredit.analytics_deencredit.util_deencredit.AnalyticsUtil_deencredit;
import com.test.deencredit.R;
import com.test.deencredit.activity_deencredit.ln_deencredit.MainActivity_deencredit;
import com.test.deencredit.base_deencredit.BaseActivity_deencredit;
import com.test.deencredit.base_deencredit.BaseApplication_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.LoginInfo_deencredit;
import com.test.deencredit.config_deencredit.PermissionConfig_deencredit;
import com.test.deencredit.util_deencredit.UserInfoUtil_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import org.json.JSONException;
import org.json.JSONObject;


public class LauncherActivity_deencredit extends BaseActivity_deencredit {

    private boolean isFirstStart_deencredit = true;

    private ActivityResultLauncher<Intent> requestPermissionArl_deencredit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher_deencredit);
        page_deencredit = "LAUNCH";

        requestPermissionArl_deencredit = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this::afterPermission_deencredit);

        findViewById(R.id.al_start).setOnClickListener(v -> startProcess_deencredit());

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isFirstStart_deencredit) startProcess_deencredit();
    }

    private void afterPermission_deencredit(ActivityResult result){
        if (result.getResultCode()==RESULT_OK) onTZResponse_deencredit();
        else if (result.getResultCode()== RESULT_OUT_APP_deencredit) finish();
    }

    private void startProcess_deencredit(){
        //1.获取时区配置
        HttpUtil_deencredit.getInstance_deencredit().requestTzConfig_deencredit(this, TimeZoneUtil_deencredit.INSTANCE.getTimeZoneID_deencredit(),this::onTZResponse_deencredit);
    }

    public void onTZResponse_deencredit(){
        //2.检查权限
        if(!PermissionConfig_deencredit.checkPhonestatePermission_deencredit(this) || (isFirstStart_deencredit && !PermissionConfig_deencredit.checkSmsPermission_deencredit(this))){
            isFirstStart_deencredit = false;
            requestPermissionArl_deencredit.launch(new Intent(this, PermissionActivity_deencredit.class));
            return;
        }

        AnalyticsUtil_deencredit.INSTANCE.init_deencredit(BaseApplication_deencredit.getInstant());

        HttpUtil_deencredit.getInstance_deencredit().requestAppSInfo_deencredit(this,this::onAppShowInfoResponse_deencredit);
    }

    public void onAppShowInfoResponse_deencredit(){

        //自动登录
        if (UserInfoUtil_deencredit.getInstance_deencredit().didLogin_deencredit()){
            try {
                JSONObject object = new JSONObject();
                object.put("token", UserInfoUtil_deencredit.getInstance_deencredit().getToken_deencredit());
                object.put("userId", UserInfoUtil_deencredit.getInstance_deencredit().getUserId_deencredit());
                object.put("type", UserInfoUtil_deencredit.LOGIN_TYPE_OLD_deencredit);
                object.put("isAdSource", UserInfoUtil_deencredit.getInstance_deencredit().getIsAdSource_deencredit() ? "YES":"NO");
                LoginInfo_deencredit.parse_deencredit(object);

                AnalyticsUtil_deencredit.INSTANCE.saveFirebaseAndFacebookS2SInfo_deencredit();
                HttpUtil_deencredit.getInstance_deencredit().userProcess_deencredit(this,this::onUserProcessResponse_deencredit);
                return;
            }catch (JSONException e){
                e.printStackTrace();
            }
        }

        Intent intent = new Intent(this, LoginActivity_deencredit.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();
    }

    public void onUserProcessResponse_deencredit(){
        startActivity(new Intent(this, MainActivity_deencredit.class));
        finish();
    }

}